package cn.zyq.baking.controller;

import cn.zyq.baking.commons.response.JsonResult;
import cn.zyq.baking.pojo.dto.ContentDTO;
import cn.zyq.baking.pojo.vo.*;
import cn.zyq.baking.service.IContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/contents")
public class ContentController {
    @Autowired
    IContentService contentService;

    @PostMapping("/add-new")
    public JsonResult addnew(ContentDTO contentDTO){
        contentService.addNew(contentDTO);
        return JsonResult.ok();
    }

    /**根据类型和用户id查询文章*/
    @GetMapping("/{type/{userId}/management}")
    public JsonResult listForManagement(@PathVariable Long type,@PathVariable Long userId){
        List<ContentManagementVO> list=contentService.listForManagement(type,userId);
        return JsonResult.ok(list);
    }

    /**根据id删除文章*/
    @PostMapping("/{id}/delete")
    public JsonResult deleteById(@PathVariable  Long id){
        contentService.deleteById(id);
        return JsonResult.ok();
    }

    /**根据id删除文章*/
    @GetMapping("/{id}/loadForUpdate")
    public JsonResult loadForUpdateById(@PathVariable  Long id){
        ContentUpdateVO contentUpdateVO=contentService.selectForUpdateById(id);
        return JsonResult.ok(contentUpdateVO);
    }

    /**根据文章类型和文章id查询文章用于： 在首页显示*/
    @RequestMapping("/{type}/{categoryId}/index")
    public JsonResult listForIndex_byTypeAndCategoryId(@PathVariable Long type,@PathVariable Long categoryId){
        List<ContentIndexVO> list=contentService.listForIndex_byTypeAndCategoryId(type,categoryId);
        return JsonResult.ok(list);
    }

    /**根据文章id查询文章详情*/
    @RequestMapping("/{id}/detail")
    public JsonResult loadDetailById(@PathVariable  Long id){
        ContentDetailVO contentDetailVO=contentService.selectDetailById(id);
        return JsonResult.ok(contentDetailVO);
    }

    /**根据用户id查询文章用于其他地方显示*/
    @RequestMapping("/{userId}/other")
    public JsonResult listForOtherByUserId(@PathVariable  Long userId){
        List<ContentSimpleVO> list=contentService.otherListByUserId(userId);
        return JsonResult.ok(list);
    }

    /**热门文章列表*/
    @RequestMapping("/hot")
    public JsonResult hotList(){
        List<ContentSimpleVO> list=contentService.hotList();
        return JsonResult.ok(list);
    }

    /**根据类型查询文章用于在首页显示*/
    @RequestMapping("/{type}/list")
    public JsonResult listForIndex_byType(@PathVariable Long type){
        List<ContentIndexVO> list=contentService.listByType(type);
        return JsonResult.ok(list);
    }

    /**根据关键字搜索文章*/
    @RequestMapping("/{word}/search")
    public JsonResult searchForIndex_byWord(@RequestParam String word){
        List<ContentIndexVO> list=contentService.searchForIndex_byWord(word);
        return JsonResult.ok(list);
    }

    /**根据类型查询文章用于给管理员显示*/
    @RequestMapping("/{type}/list-for-admin")
    public JsonResult listForAdminByType(@PathVariable Long type){
        List<ContentAdminVO> list=contentService.listForAdminByType(type);
        return JsonResult.ok(list);
    }

}
